<?php
$errer ="";
$name = "";
$phone = "";
$email = "";
$company = "";
$title = "";
$description = "";
// ถ้า user submit ฟอร์มมา
if(isset($_POST['name'])){
	// ถ้าป้อน captcha มาถูกต้อง
	if (chk_crypt($_POST['ispostback'])){
		include 'inc/reuse/dbconfig.php';
		$sql = "INSERT INTO contacts (contacts_id,contacts_from_sender,contacts_from_sender_phone,contacts_from_sender_email,contacts_from_company,
					contacts_title,contacts_description,contacts_active,contacts_reply,contacts_date_time)
				    VALUES (null,'".$_POST['name']."', '".$_POST['phone']."','".$_POST['email']."', '".$_POST['company']."', '".$_POST['title']."', '".$_POST['description']."', 1,1,'".date("YmdHis")."')";
		if(mysql_query($sql))
		{
			$sql = "SELECT emails_description FROM emails" ;
			$rsd = mysql_query($sql);
			while($row = mysql_fetch_array($rsd)){
				$header = "From: ".$_POST['email']."\r\n";
				$header .= "Content-type: text/html; charset=utf-8\r\n" ;
				$to = $row['emails_description'];
				$subject = '=?utf-8?B?'.base64_encode($_POST['title']).'?=';
				$body = nl2br(htmlspecialchars($_POST['description']));
				$flgSend = mail($to,$subject,$body,$header);
			}
			echo "redirectPageComplete();";
		}
		else echo "redirectPageIncomplete()";
	} else{
		// ถ้าป้อน captcha ไม่ถูกต้อง
		$errer =  "ตัวอักษรไม่ตรงกับรูปภาพ กรุณาพิมพ์ใหม่อีกครั้ง";
		// เอาค่าเดิมก่อนหน้ามาใส่ใน form ใหม่
		$name = $_POST['name'];
		$phone = $_POST['phone'];
		$email = $_POST['email'];
		$company = $_POST['company'];
		$title = $_POST['title'];
		$description = $_POST['description'];
	}
}
?>